﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ext.Net;
using Ext.Net.Utilities;
using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.ManagerObjects;
using System.Data;

namespace MemberMgr.Website.Member
{
    public partial class MemberSearch : Common.TabPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RowSelect(object sender, DirectEventArgs e)
        {
            uint memberID = uint.Parse(e.ExtraParams["memberID"]);

            ItMemberManager memberMgr = Common.CommonUtil.managerFactory.GettMemberManager();

            tMember member = memberMgr.GetById(memberID);

            if (member != null)
            {
                this.fpMemberDetail.SetValues(new
                {
                    member.Id,
                    member.Memberno,
                    member.Membername,
                    member.Membertel,
                    member.Memberaddress,
                    member.Membertype,
                    member.Membercard
                });
            }
        }

        protected void storeMemberList_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.storeMemberList.DataBind();
        }

        [DirectMethod]
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataSource();
        }

        private void BindDataSource()
        {
            string memberName = this.txtSearchName.Text;
            string memberNo = this.txtSearchNo.Text;

            ItMemberManager memberMgr = Common.CommonUtil.managerFactory.GettMemberManager();
            IList<tMember> list = null;

            if (!string.IsNullOrEmpty(memberName) && !string.IsNullOrEmpty(memberNo))
            {
                list = memberMgr.GetMemberByNameOrNo(memberName, memberNo);
            }
            else if (!string.IsNullOrEmpty(memberName))
            {
                list = memberMgr.GetMemberByName(memberName);
            }
            else if (!string.IsNullOrEmpty(memberNo))
            {
                list = memberMgr.GetMemberByNo(memberNo);
            }
            else
            {
                list = memberMgr.GetAll();
            }

            if (list != null)
            {
                DataTable dt = this.GetMemberInfoDataTable();

                foreach (tMember item in list)
                {
                    dt.Rows.Add(
                        item.Id,
                        item.Membername,
                        item.Memberno,
                        item.Membertel,
                        item.Memberaddress,
                        item.Membertype,
                        item.Membercard
                        );
                }

                this.storeMemberList.DataSource = dt;
                this.storeMemberList.DataBind();
            }
        }
        private DataTable GetMemberInfoDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn() { ColumnName="ID",DataType=typeof(uint)},
                new DataColumn() { ColumnName="MEMBERNAME",DataType=typeof(string)},
                new DataColumn() { ColumnName="MEMBERNO",DataType=typeof(string)},
                new DataColumn() { ColumnName="MEMBERTEL",DataType=typeof(string)},
                new DataColumn() { ColumnName="MEMBERADDRESS",DataType=typeof(string)},
                new DataColumn() { ColumnName="MEMBERTYPE",DataType=typeof(string)},
                new DataColumn() { ColumnName="MEMBERCARD",DataType=typeof(string)}
            });

            return dt;
        }

        [DirectMethod]
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string memberID = this.txtMemberID.Text.Trim();
            string memberName = this.txtMemberName.Text.Trim();
            string memberNo = this.txtMemberNo.Text.Trim();
            string memberAddress = this.txtMemberAddress.Text.Trim();
            string memberType = this.txtMemberType.Text.Trim();
            string membertel = this.txtMemberTel.Text.Trim();
            string memberCard = this.txtMemberCardNo.Text.Trim();

            //Check Data if nessiserry


            ItMemberManager memberMgr = Common.CommonUtil.managerFactory.GettMemberManager();

            memberMgr.Session.BeginTransaction();
            try
            {

                if (string.IsNullOrEmpty(memberID))
                {
                    //Add
                    tMember member = new tMember();

                    member.Membername = memberName;
                    member.Memberno = memberNo;
                    member.Memberaddress = memberAddress;
                    member.Membertel = membertel;
                    member.Membertype = memberType;
                    member.Membercard = memberCard;

                    memberMgr.Save(member);

                    memberMgr.Session.CommitTransaction();
                }
                else
                {
                    //Update
                    tMember member = memberMgr.GetById(uint.Parse(memberID));

                    if (member != null)
                    {
                        member.Membername = memberName;
                        member.Memberno = memberNo;
                        member.Memberaddress = memberAddress;
                        member.Membertel = membertel;
                        member.Membertype = memberType;
                        member.Membercard = memberCard;

                        memberMgr.Update(member);

                        memberMgr.Session.CommitTransaction();
                    }
                }

                this.BindDataSource();

            }
            catch (Exception ex)
            {
                memberMgr.Session.RollbackTransaction();

                Ext.Net.MessageBox errMsg = new Ext.Net.MessageBox();

                errMsg.Show(new MessageBoxConfig()
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR,
                    Title = "数据保存失败",
                    Message = ex.Message
                });
            }
        }

        [DirectMethod]
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtMemberID.Text.Trim()))
            {
                Ext.Net.MessageBox errMsg = new Ext.Net.MessageBox();

                errMsg.Show(new MessageBoxConfig()
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.INFO,
                    Title = "提示",
                    Message = "请先选择会员后再删除!"
                });

                return;
            }

            ItMemberManager memberMgr = Common.CommonUtil.managerFactory.GettMemberManager();
            try
            {

                memberMgr.Session.BeginTransaction();

                tMember member = memberMgr.GetById(uint.Parse(this.txtMemberID.Text.Trim()));

                if (member != null)
                {
                    memberMgr.Delete(member);

                    memberMgr.Session.CommitTransaction();

                    this.BindDataSource();
                }

            }
            catch (Exception ex)
            {
                memberMgr.Session.RollbackTransaction();

                Ext.Net.MessageBox errMsg = new Ext.Net.MessageBox();

                errMsg.Show(new MessageBoxConfig()
                {
                    Buttons = MessageBox.Button.OK,
                    Icon = MessageBox.Icon.ERROR,
                    Title = "数据保存失败",
                    Message = ex.Message
                });
            }
        }
    }
}
